<?php
/**
 * Created by PhpStorm.
 * User: zb
 * Date: 15-6-9
 * Time: 下午2:36
 */
class LoginController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle(" 用户登录");
        parent::initialize();
    }

    public function indexAction()
    {
        $this->tag->setDefault("username", "admin");
        $this->tag->setDefault("password", "phalcon");
    }

    public function _registerSession(Ausers $user)
    {
        $this->session->set('auth', array(
            'id' => $user->id,
            'name' => $user->name
        ) );
    }

    public function startAction()
    {
        if ($this->request->isPost()) {

            $username = $this->request->getPost('username');
            $password = $this->request->getPost('password');

            $conditions = " username = :username: AND password = :password: ";
            $parameters = array('username' => "$username", "password" => md5($password));
            $user = Ausers::findFirst(array($conditions, 'bind' => $parameters));

            if ($user != false) {
                $this->_registerSession($user);
                $this->flash->success("欢迎 '" . $user->name . "'来到统一登录平台！");
                return $this->forward('businesssystems/search');
            }

            $this->flash->error('用户或密码不正确！');
            return $this->forward('login/index');
        }
    }

    public function endAction()
    {
        $this->session->remove('auth');
        $this->flash->success('注销成功！');
        return $this->forward('login/index');
    }
}
